package main.java.com.action;

import java.sql.Connection;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import main.java.com.domain.User;
import main.java.com.util.ConnDb;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * !!! Anti-fake label !!!
 * ClassName: LoginAction
 * Package: main.java.com.action
 * Description:
 *
 * @Author 22软件6班 陈彬
 * @Create 2023/11/6 19:35
 * @Version 1.0
 **/

@WebServlet("/LoginServlet")
public class LoginAction extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //配置请求和响应的字符集为UTF-8
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String loginId = request.getParameter("loginId");
        String password = request.getParameter("password");
        //调用账号和密码查询信息方法
        User user = this.queryUserInfo(loginId,password);
        //创建一个jsnobject对象
        JSONObject jsonobject = new JSONObject();
        if (user!=null){
            //若登录成功将loginId参数写入到Session会话存储中
            request.getSession().setAttribute("loginId",loginId);
            jsonobject.put("code",1);//code=1,有权限
            jsonobject.put("msg","有权限登录系统！");
        }else {
            jsonobject.put("code",-1);//code=-1,没有权限
            jsonobject.put("msg","没有权限登录系统！");
        }
        //将jsonobject转化为字符串
        String json = JSON.toJSONString(jsonobject);
        //将json字符串写入响应信息中
        response.getWriter().write(json);
    }
    //根据账号和密码查询信息
    public User queryUserInfo(String loginId,String password){
        User user = null;
        try{
            //调用连接数据库方法
            ConnDb db = new ConnDb();
            Connection conn = db.dataBaseConnectionTest();
            //构建sql
            String sql = "SELECT * FROM tb_sys_user WHERE loginId=? AND password=?";
            //利用PreparedStatement预处理接口创建对象pstm
            PreparedStatement pstm = conn.prepareStatement(sql);
            //为占位符？赋值
            pstm.setString(1,loginId);//为第1个问号赋值
            pstm.setString(2,password);//为第2个问号赋值
            //执行方法
            ResultSet rs = pstm.executeQuery();
            while (rs.next()){//如果查询到数据，则进入while循环
                user = new User();//创建一个bean实体类
                user.setLoginId(rs.getString("loginId"));
                user.setPassword(rs.getString("password"));
            }
            conn.close();//关闭数据库
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    //根据账号查询用户注册信息
    public User queryUserInfoByLoginId(String loginId){
        User user=new User();
        try {
            //调用连接数据库方法
            ConnDb db = new ConnDb();
            Connection conn = db.dataBaseConnectionTest();
            //构建SQL语句
            String sql1="SELECT a.loginId,a.`password`,b.userName,b.phone,b.email,b.registType,b.sex,DATE_FORMAT(b.updateTime,'%Y-%m-%d %H:%i:%s')as updateTime\n" +
                    "FROM tb_sys_user a\n" +
                    "LEFT JOIN tb_sys_staff_info b ON a.loginId=b.loginId\n" +
                    "WHERE a.loginId=?";
            //利用PreparedStatement预处理接口创建对象pstm
            PreparedStatement pstm = conn.prepareStatement(sql1);
            //为占位符?赋值
            pstm.setString(1,loginId);//为第1个问号赋值
            //执行方法
            ResultSet rs = pstm.executeQuery();
            while (rs.next()){
                user.setLoginId(rs.getString("loginId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setRegistType(rs.getString("registType"));
                user.setSex(rs.getString("sex"));
                user.setUpdateTime(rs.getString("updateTime"));
            }
            conn.close();//关闭数据库
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }
}
